package com.Scheduled;


import com.lock.RedisLockUtils;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.UUID;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务:默认为单线程，开启多个任务时，任务的执行时机会受上一个任务执行时间的影响
public class SaticScheduleTask {

    @Autowired
    RedisLockUtils redisLockUtils ;

    @Value("${server.port}")
    private  String port ;

    //@Scheduled(cron = "0/5 * * * * ?")
    private void configureTasks1() throws InterruptedException {

        String uuid= UUID.randomUUID().toString();
        boolean lock = redisLockUtils.lock("lock", uuid, 200);
        try {
            if(lock){
                System.err.println("configureTasks1定时任务时间: " + LocalDateTime.now()+"===线程名称"+Thread.currentThread().getName()+"端口"+this.port+"=uuid=="+uuid);
                Thread.sleep(30000);
            }
        } finally {
            redisLockUtils.unlockScript("lock",uuid);
        }


    }
    //@Scheduled(cron = "0/10 * * * * ?")
    private void configureTasks2() {
        System.err.println("configureTasks2执行静态定时任务时间: " + LocalDateTime.now()+"===线程名称"+Thread.currentThread().getName());
    }
}
